// pages/brokerage/brokerage.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgPath:app.globalData.imgPath,
    account:0,
    rebate:0,
    defaulbankId:0,
    canuse:0,
    hasCar:true,
    cardInfo:null,
    money:0,
    limit:null
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    wx.request({
      url: app.globalData.reqPath + 'shop/Members',
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data }) {
        if (data.code == 200) {
          self.setData({ rebate: data.data.totalInterest, canuse: data.data.interest,})
          // self.setData({ rebate: data.data.interest, defaulbankId: data.data.bankId });
        }
      }
    })
    if (options.cardId) {
      //  console.log("tyouyyinhangk ")
      wx.request({
        url: app.globalData.reqPath + 'shop/MembersBanks/' + options.cardId,
        header: {
          'authorization': app.globalData.authorization
        },
        success: function ({ data }) {
          console.log(data.data)
          self.setData({ cardInfo: data.data, hasCar: true })
        }
      })
    } else {
      wx.request({
        url: app.globalData.reqPath + 'shop/MembersBanks',
        header: {
          'authorization': app.globalData.authorization
        },
        success: function ({ data }) {
          let list = data.data;
            if(list.length>0){
            let defaultCard = list.filter(function (item) {
              return item.isDefault;
            })
            if (defaultCard.length > 0) {
              self.setData({ cardInfo: defaultCard[0] })
              console.log(defaultCard)
            }else{
              self.setData({ cardInfo: list[0] })
            }
          }else{
              self.setData({ hasCar:false})
          }
        }
      })
    }  
    wx.request({
      url: app.globalData.reqPath +'shop/MembersCash/setting',
      header: {
        'authorization': app.globalData.authorization
      },
      method:'POST',
      success:function({data}){
        //console.log(data)
        let limit=data.data;
        self.setData({limit:limit})
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inputmoney:function(e){
    this.setData({money:e.detail.value})
  },
  confrim: function () {//确定提现
    if (this.data.money > this.data.canuse || this.data.money <= 0) {
        wx.showToast({
          title: '请输入正确的金额',
          icon: 'none'
        })
        return false;
      }
      wx.request({
        url: app.globalData.reqPath + 'shop/MembersCash',
        data: {
          bankId: this.data.cardInfo.id,
          money: this.data.money
        },
        header: {
          'authorization': app.globalData.authorization
        },
        method: 'POST',
        success: function ({ data }) {
          if (data.code == 200) {
            wx.showToast({
              title: '提现申请成功',
              icon: 'none',
              success: function () {
                wx.redirectTo({
                  url: '/pages/brokerageDetail/brokerageDetail',
                })
              }
            })
          } else {
            wx.showToast({
              title: '请输入正确的金额',
              icon: 'none'
            })
          }
        }
      })
    }
  
})